games.game7 = {
    //snakes and ladders
    //Render big 10x10 table
    init: function () {
        var html = "<table id='game7-snakes-ladders'>";
        html += "<tr><td colspan='10'><h1>Roll Dice</h1><div class='dice-value'>?</div><div class='piece-holder'></div></td></td>";

        for (var i = 9; i >= 0; i--) {
            html += "<tr>";
            var maxNo = 0;
            for (var k = 9; k >= 0; k--) {
                var rowVal = (i * 10) + (k == 10 ? 0 : k) + 1;
                maxNo = rowVal % 10 == 0 ? rowVal : maxNo;
                rowVal = i % 2 == 0 ? ((rowVal * (-1)) + (-9) + (maxNo * 2)) : rowVal;
                html += "<td class='cell{0}'>&nbsp;</td>".replace("{0}", rowVal);
            }
            html += "</tr>";
        }
        html += "</table>";
        $("#gamezone").html(html);
        $(".piece-holder").append($("<img src='http://12-games-of-christmas.googlecode.com/svn/trunk/assets/player1.png' style='float:left;' class='player'/>"));
        $(".piece-holder").append($("<img src='http://12-games-of-christmas.googlecode.com/svn/trunk/assets/player2.png' style='float:left;' class='npc'/>"));

        $("#game7-snakes-ladders h1").click(function () {
            if (games.game7.userLockOut) return;
            games.game7.userLockOut = true;
            var diceValue = games.game7.rollDice();
            games.game7.player[1] += diceValue;
            games.game7.movePiece();
            setTimeout(function () {
                games.game7.npcRoll();
            }, diceValue * 300);
        });
    },
    userLockOut: false,
    npcRoll: function () {
        games.game7.npc[1] += games.game7.rollDice();
        games.game7.movePiece();
        games.game7.userLockOut = false;
    },
    rollDice: function () {
        games.game7.diceValue = games.helper.getRandomNumber(1, 6);
        $("#game7-snakes-ladders .dice-value").text(games.game7.diceValue);
        return games.game7.diceValue;
    },
    isPlayersGo: true,
    moveTicker: undefined,
    diceValue: 0,
    player: [1, 1],
    npc: [1, 1],
    isActionCell: function (cellNo, element, playerCoords) {
        cellNo--;
        switch (cellNo) {
            case 1: //promote to 38
                $(".cell{0}".replace("{0}", 38)).append(element);
                playerCoords[0] = 38; playerCoords[1] = 38;
                break;
            case 4: //promote to 14
                $(".cell{0}".replace("{0}", 14)).append(element);
                playerCoords[0] = 14; playerCoords[1] = 14;
                break;
            case 9: //promote to 31
                $(".cell{0}".replace("{0}", 31)).append(element);
                playerCoords[0] = 31; playerCoords[1] = 31;
                break;
            case 28: //promote to 84
                $(".cell{0}".replace("{0}", 84)).append(element);
                playerCoords[0] = 84; playerCoords[1] = 84;
                break;
            case 21: //promote to 42
                $(".cell{0}".replace("{0}", 42)).append(element);
                playerCoords[0] = 42; playerCoords[1] = 42;
                break;
            case 51: //promote to 67
                $(".cell{0}".replace("{0}", 67)).append(element);
                playerCoords[0] = 67; playerCoords[1] = 67;
                break;
            case 71: //promote to 91
                $(".cell{0}".replace("{0}", 91)).append(element);
                playerCoords[0] = 91; playerCoords[1] = 91;
                break;
            case 80: //promote to 100
                $(".cell{0}".replace("{0}", 100)).append(element);
                playerCoords[0] = 100; playerCoords[1] = 100;
                break;
            case 17: //relegate to 38
                $(".cell{0}".replace("{0}", 7)).append(element);
                playerCoords[0] = 7; playerCoords[1] = 7;
                break;
            case 62: //relegate to 14
                $(".cell{0}".replace("{0}", 19)).append(element);
                playerCoords[0] = 19; playerCoords[1] = 19;
                break;
            case 87: //relegate to 31
                $(".cell{0}".replace("{0}", 24)).append(element);
                playerCoords[0] = 24; playerCoords[1] = 24;
                break;
            case 54: //relegate to 84
                $(".cell{0}".replace("{0}", 34)).append(element);
                playerCoords[0] = 34; playerCoords[1] = 34;
                break;
            case 64: //relegate to 42
                $(".cell{0}".replace("{0}", 60)).append(element);
                playerCoords[0] = 60; playerCoords[1] = 60;
                break;
            case 93: //relegate to 67
                $(".cell{0}".replace("{0}", 73)).append(element);
                playerCoords[0] = 73; playerCoords[1] = 73;
                break;
            case 95: //relegate to 91
                $(".cell{0}".replace("{0}", 75)).append(element);
                playerCoords[0] = 75; playerCoords[1] = 75;
                break;
            case 98: //relegate to 100
                $(".cell{0}".replace("{0}", 79)).append(element);
                playerCoords[0] = 79; playerCoords[1] = 79;
                break;
        }
    },
    movePiece: function () {
        games.game7.moveTicker = setInterval(function () {
            if (games.game7.isPlayersGo) {
                if (games.game7.player[0] == games.game7.player[1]) {
                    clearInterval(games.game7.moveTicker);
                    games.game7.isActionCell(games.game7.player[0], $("#game7-snakes-ladders .player"), games.game7.player);
                    if (games.game7.player[0] == 100) {
                        setInterval(function () { clearInterval(games.game7.moveTicker); }, 1);
                        alert("You WON!!!!! Come back tomorow for some more.");
                    }
                    games.game7.isPlayersGo = false;
                } else {
                    $(".cell{0}".replace("{0}", games.game7.player[0]++)).append($("#game7-snakes-ladders .player"));
                    games.game7.player[0] = games.game7.player[0] == 100 ? 100 : games.game7.player[0];
                    games.game7.player[1] = games.game7.player[1] == 100 ? 100 : games.game7.player[1];
                }
            } else {
                if (games.game7.npc[0] == games.game7.npc[1]) {
                    setInterval(function () { clearInterval(games.game7.moveTicker); }, 1);
                    games.game7.isActionCell(games.game7.npc[0], $("#game7-snakes-ladders .npc"), games.game7.npc);
                    if (games.game7.npc[0] == 100) {
                        setInterval(function () { clearInterval(games.game7.moveTicker); }, 1);
                        alert("You loose, why not try again.");
                    }
                    games.game7.isPlayersGo = true;
                } else {
                    $(".cell{0}".replace("{0}", games.game7.npc[0]++)).append($("#game7-snakes-ladders .npc"));
                    games.game7.npc[0] = games.game7.npc[0] == 100 ? 100 : games.game7.npc[0];
                    games.game7.npc[1] = games.game7.npc[1] == 100 ? 100 : games.game7.npc[1];
                }
            }
        }, 250);
    }
};